// let url = require('url');
const user = require('./../server/userServer');
const CryptoJS = require('crypto-js');
let userSverice = new user();

function log_reg_Server() {
    let result = {};
    this.register = (req, res) => {
        //let params = url.parse(req.url, true).query; //解析数据，格式为对象
        let account = req.body.account;
        userSverice.queryAccount(account, rows => {
            if (rows.length === 0) {
                userSverice.register(req.body, rows => {
                    //处理结果
                    if (rows) {
                        result.code = 1;
                        result.data = rows;
                        res.write(JSON.stringify(result));
                        res.end();
                    }
                })
            } else {
                //若返回数据长度不为0，说明账号已经被注册过了
                result.code = 0;
                result.msg = '该账号已经被注册，换个账号试试呗';
                res.write(JSON.stringify(result));
                res.end();
            }
        })
    }
    this.login = (req, res) => {
        //let params = url.parse(req.url, true).body; //解析数据，格式为对象
        let account = req.body.account,
            psd = req.body.psd;
        userSverice.login(account, rows => {
            if (rows.length > 0) {
                if (rows[0].psd !== psd) {
                    result.code = 0;
                    result.msg = '密码错误';
                } else {
                    let cookies = {
                        account: req.body.account
                    }
                    req.session.userinfo = cookies;
                    result.code = 1;
                    result.data = rows[0];
                }
                res.write(JSON.stringify(result));
                res.end();
            } else {
                result.code = 0;
                result.is_login = 0;
                result.msg = '账户不存在，请检查账号是否正确';
                res.write(JSON.stringify(result));
                res.end();
            }
        })
    }
    this.getUser = (req, res) => {
        // 单点登录用户信息
        let account = '',
            result = {};
        console.log(req.session);
        if (req.session.userinfo === undefined) {
            if (req.query && req.query.callback) {
                var data = {
                    code: 0,
                    message: '登录信息已超时，请重新登录'
                }
                result = req.query.callback + '(' + JSON.stringify(data) + ')';
                console.log(result);
            } else {
                result.code = -1;
                result.msg = '登录信息已超时，请重新登录';
            }
            res.write(JSON.stringify(result));
            res.end();
        } else {
            account = req.session.userinfo.account;
            userSverice.login(account, rows => {
                if (rows) {
                    if (req.query && req.query.callback) {
                        var data = {},
                            user = {};
                        data.is_login = 1;
                        user.user_id = rows[0].id; // 用户Id
                        user.nickname = rows[0].nickName; // 昵称
                        user.img_url = rows[0].portrait; // 头像
                        user.profile_url = '/user?userid=' + rows[0].id;
                        data.user = user;
                        // 签名算法
                        var str = 'img_url=' + rows[0].portrait + '&nickname=' + rows[0].nickName + '&profile_url=' + user.profile_url + '&user_id=' + rows[0].id;
                        const app_key = 'REzySUKRCPfyfV/jfgwTA==';
                        const sign = CryptoJS.HmacSHA1(str, app_key).toString();
                        data.user.sign = sign;
                        result = req.query.callback + '(' + JSON.stringify(data) + ')'; //jsonp
                    } else {
                        result.code = 1;
                        result.data = rows;
                    }
                    res.write(JSON.stringify(result));
                    res.end();
                }
            })
        }
    }
    this.upload = (req, res) => {
        userSverice.queryAccount(req.body.account, rows => {
            if (rows.length > 0) {
                userSverice.upload(req.body, rows => {
                    if (rows) {
                        result.code = 1;
                        result.data = rows;
                        res.write(JSON.stringify(result));
                        res.end();
                    }
                })
            } else {
                result.code = 0;
                result.msg = 'id有误';
                res.write(JSON.stringify(result));
                res.end();
            }

        })
    }
    this.queryUsers = (req, res) => {
        // console.log(req.cookies);
        userSverice.queryAllUsers(rows => {
            if (rows) {
                result.code = 1;
                result.data = rows;
            } else {
                result.code = 0;
                result.data = null
            }
            res.write(JSON.stringify(result));
            res.end();
        })
    }
    this.frezee = (req, res) => {
        userSverice.frezee(req.body, rows => {
            if (rows) {
                result.code = 1;
                result.data = rows;
            } else {
                result.code = 0;
                result.data = null;
            }
            res.write(JSON.stringify(result));
            res.end();
        })
    }
    this.batchFrezee = (req, res) => {
        let data = req.body.users;
        console.log(data);
        userSverice.batch(data, rows => {
            if (rows) {
                result.code = 1;
                result.data = rows;
            } else {
                result.code = 0;
                result.data = null;
            }
            res.write(JSON.stringify(result));
            res.end();
        })
    }
    this.search = (req, res) => {
        if (req.query.key === '' || req.query.key === undefined) {
            result.code = 0;
            result.msg = '请选择要查询的字段';
            res.write(JSON.stringify(result));
            res.end();
            return;
        } else if (req.query.value === '') {
            result.code = 0;
            result.msg = '请选择要查询的关键字';
            res.write(JSON.stringify(result));
            res.end();
            return;
        } else {
            userSverice.search(req.query, rows => {
                if (rows) {
                    result.code = 1;
                    result.data = rows;
                } else {
                    result.code = 0;
                    result.data = null;
                }
                res.write(JSON.stringify(result));
                res.end();
            })
        }
    }
    this.setinfo = (req,res) => {
        userSverice.setInfo(req, rows => {
            if (rows) {
                result.code = 1;
                result.msg = '设置成功！';
            } else {
                result.code = 0;
                result.data = null;  
            }
            res.write(JSON.stringify(result));
            res.end();
        })
    }
}

/*导出loginServer*/
module.exports = log_reg_Server;